<?php

use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class DuomaiController extends ControllerBase
{
    public function initialize()
    {
        parent::initialize();
    }

    public function planlistAction()
    {
        $params['page']     = isset($this->params['page']) ? $this->params['page'] : 1;
        $params['limit']    = isset($this->params['limit']) ? $this->params['limit'] : 10;
        $params['keywords'] = isset($this->params['keywords']) ? $this->params['keywords'] : '';
        $duomai_goods_site  = new DuomaiGoodsSite();
        $list               = $duomai_goods_site->get_list($params);
        if(!empty($list['list'])){
            foreach ($list['list'] as &$val){
                $h_params['site_url'] = $val['site_url'];
                $h_params['user_id'] = '';
                $val['turn_url'] = $this->duomai->gettopromote($h_params);
            }

        }
        $this->ok($list);
    }

    //商家推广计划查询
    public function goodser_planAction()
    {

        $res = $this->duomai->goodser_plan();
        foreach ($res as $key => $value) {
            //$find_site_obj = $duomaigoods->find_by_site_url($value['site_url']);
            $duomaigoods = new DuomaiGoodsSite();
            $duomaigoods = $duomaigoods->findFirstByAdsId($value['ads_id']);
            if (!$duomaigoods) {
                $duomaigoods = new DuomaiGoodsSite();
            }
            $duomaigoods->site_url         = $value['site_url'];
            $duomaigoods->site_description = $value['site_description'];
            $duomaigoods->site_logo        = $value['site_logo'];
            $duomaigoods->ads_commission   = $value['ads_commission'];
            $duomaigoods->ads_endtime      = $value['ads_endtime'];
            $duomaigoods->status           = $value['status'];
            $cate_name                     = is_array($value['cate_name']) ? '' : $value['cate_name'];
            if (empty($cate_name)) {
                $cate_name = $value['ads_name'];
            }
            $duomaigoods->cate_name = $cate_name;
            $duomaigoods->note      = is_array($value['note']) ? json_encode($value['note']) : $value['note'];
            $duomaigoods->ads_id    = $value['ads_id'];
            $duomaigoods->cate_id   = rand(1, 4);        //此段代码用于测试
            $duomaigoods->save();
        }
        $this->ok();
    }


    //商家促销信息查询
    public function goodser_salespromotionAction()
    {
        $res = $this->duomai->goodser_salespromotion();
    }

    //商家高佣商品查询
    public function highcommissionAction()
    {
        $res = $this->duomai->highcommission();
    }

    //获取推广链接
    public function gettopromoteAction()
    {
        if (!isset($this->params['user_id']) || empty($this->params['user_id'])) {
            $this->fail(-1, '用户id不能为空');
        }
        if (!isset($this->params['site_url']) || empty($this->params['site_url'])) {
            $this->fail(-1, '站点url不能为空');
        }
//        if(!isset($this->params['aid']) || empty($this->params['aid'])){
//            $this->fail(-1,'推广计划id不能为空');
//        }
        $res['return_url'] = $this->duomai->gettopromote($this->params);
        $this->ok($res);
    }

    /*京东推广链接获取
     * site_url推广京东地址（UrlEncode编码)可以是商品地址，活动地址，商品优惠券地址。转商品二合一地址，请填 商品优惠券地址
     * skuid 京东商品skuid，转商品二合一地址时必填
    */

    public function jdtopromoteurlAction()
    {
        if (!isset($this->params['user_id']) || empty($this->params['user_id'])) {
            $this->fail(-1, '用户id不能为空');
        }
        if (!isset($this->params['aid']) || empty($this->params['aid'])) {
            $this->fail(-1, '	推广计划ID不能为空');
        }
        if (!isset($this->params['site_url']) || empty($this->params['site_url'])) {
            //$this->fail(-1,'请输入京东商品地址、活动地址、商品优惠价地址');
        }
        if (!isset($this->params['skuid']) || empty($this->params['skuid'])) {
            //$this->fail(-1,'京东商品skuid，');
        }
        $res = $this->duomai->jdtopromoteurl($this->params);
    }



    //拼多多推广链接获取
    //推广拼多多地址（UrlEncode编码)可以是商品地址，活动地址。
    public function pddtopromoteurlAction()
    {
        if (!isset($this->params['user_id']) || empty($this->params['user_id'])) {
            $this->fail(-1, '用户id不能为空');
        }
        if (!isset($this->params['aid']) || empty($this->params['aid'])) {
            $this->fail(-1, '	推广计划ID不能为空');
        }
        if (!isset($this->params['site_url']) || empty($this->params['site_url'])) {
            //$this->fail(-1,'请输入京东商品地址、活动地址、商品优惠价地址');
        }
        if (!isset($this->params['skuid']) || empty($this->params['skuid'])) {
            //$this->fail(-1,'京东商品skuid，');
        }
        $res = $this->duomai->pddtopromoteurl($this->params);
    }


    public function testordeAction()
    {
        $h_params['order_sn'] = '33445657811';
        $order_detail         = $this->duomai->order_detail_info($h_params);
        var_dump($order_detail);
    }
}
